// 作业1:使用open write close的形式去创建一个大文件,内容从指定的文本中随机取一些（每次取得长度内容可以不一样）,计算出这个大文件出现最多的字符(要使用流的形式).


let str="换入的恢复度法发动/.,msjkhudfhdfdjfhv852132654华为胡地方保护与方式杜师傅师傅杜师傅机";
//使用open write close的形式去创建一个大文件
let fs=require("fs");
let fd=fs.openSync('./big.txt','w');
for(let i=0;i<10000;i++){
    //创建随机长度；
    let size=Math.ceil(Math.random()*30);
    //console.log(size);
    
    let rq='';
    //随机取不同长度的文本
    for(let i=0;i<size;i++){
     
        let suiji=Math.ceil(Math.random()*str.length);
        rq+=str.charAt(suiji);
    }
//console.log(rq);
    fs.writeSync(fd,rq,{flag:'a'});
}

//关闭
fs.closeSync(fd);

//计算出这个大文件出现最多的字符(要使用流的形式)
let shuzu=[];
let rs=fs.createReadStream('./big.txt','utf-8');
rs.on("data",(chunk)=>{
    console.log(chunk);
    let neir=chunk;
    for(let j=0;j<neir.length;j++){
        if(shuzu[neir.charAt(j)]!=undefined){
            shuzu[neir.charAt(j)]+=1;
        }else{
            shuzu[neir.charAt(j)]=1
        }
    }
    let max=0;
    let zhi='';
    
    for (let key in shuzu) {
       if(shuzu[key]>max){
            max=shuzu[key];
            zhi=key;
       }
    }
    console.log(shuzu);
   console.log("此次文本中出现最多的字符是："+zhi+",共出现了"+max+"次")
});
rs.on("end",()=>{
    console.log("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++读完了+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
    
})